package es.android.supermanager;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

//http://jornadavirtual.acb.com/htm/resumen/r226.htm

public class HacerEquipo extends Activity {

	private InsertarDatos insertar;
	private Jugador [] bases, aleros, pivots;
	String [] cad_bases, cad_aleros, cad_pivots;
	static Cursor cursor;
    private static final int BUTTON_ID = Menu.FIRST;
    private static OnItemSelectedListener listener;

	Spinner base1, base2, base3, alero1, alero2, alero3, alero4, pivot1, pivot2, pivot3, pivot4;
	
    private static String[] cadenas;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		setContentView(R.layout.equipo);

		new Handler();
		insertar = new InsertarDatos();

		bases = insertar.sacarDatos("bases");
		aleros = insertar.sacarDatos("aleros");
		pivots = insertar.sacarDatos("pivots");
		
		
		int i=0;
		
		while (bases[i]!=null){
			i++;
		}

		cad_bases = new String[i];
		
		i=0;
		
		while (bases[i]!=null){
			cad_bases[i] = bases[i].getNombre();
			i++;
		}

		i=0;

		while (aleros[i]!=null){
			i++;
		}

		cad_aleros = new String[i];
		i=0;

		while (aleros[i]!=null){
			cad_aleros[i] = aleros[i].getNombre();
			i++;
		}

		i=0;
		
		while (pivots[i]!=null){
			i++;
		}

		cad_pivots = new String[100];
		i=0;
		
		while (pivots[i]!=null){
			cad_pivots[i] = pivots[i].getNombre();
			i++;
		}
	 
		ArrayAdapter<String> adapt_bases = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, cad_bases);
		ArrayAdapter<String> adapt_aleros = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, cad_aleros);
		ArrayAdapter<String> adapt_pivots = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, cad_pivots);
		
		base1 = (Spinner)findViewById(R.id.base1);
		base2 = (Spinner)findViewById(R.id.base2);
		base3 = (Spinner)findViewById(R.id.base3);
		alero1 = (Spinner)findViewById(R.id.alero1);
		alero2 = (Spinner)findViewById(R.id.alero2);
		alero3 = (Spinner)findViewById(R.id.alero3);
		alero4 = (Spinner)findViewById(R.id.alero4);
		pivot1 = (Spinner)findViewById(R.id.pivot1);
		pivot2 = (Spinner)findViewById(R.id.pivot2);
		pivot3 = (Spinner)findViewById(R.id.pivot3);
		pivot4 = (Spinner)findViewById(R.id.pivot4);
		
		base1.setOnItemSelectedListener(new OnItemSelectedListener() {
		    @Override
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	TextView tv;
    			tv = (TextView) findViewById(R.id.valb1);
    			tv.setText(String.valueOf(bases[(int)id].getValSM()));
    			sumar();
		    }
		    @Override
		    public void onNothingSelected(AdapterView<?> parentView) {

		    }
		});
		base2.setOnItemSelectedListener(new OnItemSelectedListener() {
		    @Override
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	TextView tv;
    			tv = (TextView) findViewById(R.id.valb2);
    			tv.setText(String.valueOf(bases[(int)id].getValSM()));
    			sumar();
		    }
		    @Override
		    public void onNothingSelected(AdapterView<?> parentView) {

		    }
		});
		base3.setOnItemSelectedListener(new OnItemSelectedListener() {
		    @Override
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	TextView tv;
    			tv = (TextView) findViewById(R.id.valb3);
    			tv.setText(String.valueOf(bases[(int)id].getValSM()));
    			sumar();
		    }
		    @Override
		    public void onNothingSelected(AdapterView<?> parentView) {

		    }
		});
		alero1.setOnItemSelectedListener(new OnItemSelectedListener() {
		    @Override
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	TextView tv;
    			tv = (TextView) findViewById(R.id.vala1);
    			tv.setText(String.valueOf(aleros[(int)id].getValSM()));
    			sumar();
		    }
		    @Override
		    public void onNothingSelected(AdapterView<?> parentView) {

		    }
		});
		alero2.setOnItemSelectedListener(new OnItemSelectedListener() {
		    @Override
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	TextView tv;
    			tv = (TextView) findViewById(R.id.vala2);
    			tv.setText(String.valueOf(aleros[(int)id].getValSM()));
    			sumar();
		    }
		    @Override
		    public void onNothingSelected(AdapterView<?> parentView) {

		    }
		});
		alero3.setOnItemSelectedListener(new OnItemSelectedListener() {
		    @Override
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	TextView tv;
    			tv = (TextView) findViewById(R.id.vala3);
    			tv.setText(String.valueOf(aleros[(int)id].getValSM()));
    			sumar();
		    }
		    @Override
		    public void onNothingSelected(AdapterView<?> parentView) {

		    }
		});
		alero4.setOnItemSelectedListener(new OnItemSelectedListener() {
		    @Override
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	TextView tv;
    			tv = (TextView) findViewById(R.id.vala4);
    			tv.setText(String.valueOf(aleros[(int)id].getValSM()));
    			sumar();
		    }
		    @Override
		    public void onNothingSelected(AdapterView<?> parentView) {

		    }
		});
		pivot1.setOnItemSelectedListener(new OnItemSelectedListener() {
		    @Override
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	TextView tv;
    			tv = (TextView) findViewById(R.id.valp1);
    			tv.setText(String.valueOf(pivots[(int)id].getValSM()));
    			sumar();
		    }
		    @Override
		    public void onNothingSelected(AdapterView<?> parentView) {

		    }
		});
		pivot2.setOnItemSelectedListener(new OnItemSelectedListener() {
		    @Override
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	TextView tv;
    			tv = (TextView) findViewById(R.id.valp2);
    			tv.setText(String.valueOf(pivots[(int)id].getValSM()));
    			sumar();
		    }
		    @Override
		    public void onNothingSelected(AdapterView<?> parentView) {

		    }
		});
		pivot3.setOnItemSelectedListener(new OnItemSelectedListener() {
		    @Override
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	TextView tv;
    			tv = (TextView) findViewById(R.id.valp3);
    			tv.setText(String.valueOf(pivots[(int)id].getValSM()));
    			sumar();
		    }
		    @Override
		    public void onNothingSelected(AdapterView<?> parentView) {

		    }
		});
		pivot4.setOnItemSelectedListener(new OnItemSelectedListener() {
		    @Override
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
		    	TextView tv;
    			tv = (TextView) findViewById(R.id.valp4);
    			tv.setText(String.valueOf(pivots[(int)id].getValSM()));
    			sumar();
		    }
		    @Override
		    public void onNothingSelected(AdapterView<?> parentView) {

		    }
		});
		 
		adapt_bases.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		adapt_aleros.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		adapt_pivots.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		 
		base1.setAdapter(adapt_bases);
		base2.setAdapter(adapt_bases);
		base3.setAdapter(adapt_bases);
		alero1.setAdapter(adapt_aleros);
		alero2.setAdapter(adapt_aleros);
		alero3.setAdapter(adapt_aleros);
		alero4.setAdapter(adapt_aleros);
		pivot1.setAdapter(adapt_pivots);
		pivot2.setAdapter(adapt_pivots);
		pivot3.setAdapter(adapt_pivots);
		pivot4.setAdapter(adapt_pivots);
		
		base1.getSelectedItemPosition();
		
		super.onCreate(savedInstanceState);
		
//	    PartidoParser partido = new PartidoParser();
//		partido.setJugadores(bases, aleros, pivots);
//	    partido.leerJS(244);
//	    partido.leerJS(245);
//	    partido.leerJS(246);
//	    partido.leerJS(247);
//	    partido.leerJS(248);
//	    partido.leerJS(249);
//	    partido.leerJS(250);
//	    partido.leerJS(251);
//	    partido.leerJS(252);
	    
//	    Log.d("Base",String.valueOf(bases[1].getVal()));
//	    Log.d("Alero",String.valueOf(aleros[1].getVal()));
//	    Log.d("Pivot",String.valueOf(pivots[1].getVal()));
//	    Log.d("Pivot",String.valueOf(pivots[1].getVal()));
	    
	    TextView tv = (TextView) findViewById(R.id.valb1);
	    tv.setText("0");
	    tv = (TextView) findViewById(R.id.valb2);
	    tv.setText("0");
	    tv = (TextView) findViewById(R.id.valb3);
	    tv.setText("0");
	    tv = (TextView) findViewById(R.id.vala1);
	    tv.setText("0");
	    tv = (TextView) findViewById(R.id.vala2);
	    tv.setText("0");
	    tv = (TextView) findViewById(R.id.vala3);
	    tv.setText("0");
	    tv = (TextView) findViewById(R.id.vala4);
	    tv.setText("0");
	    tv = (TextView) findViewById(R.id.valp1);
	    tv.setText("0");
	    tv = (TextView) findViewById(R.id.valp2);
	    tv.setText("0");
	    tv = (TextView) findViewById(R.id.valp3);
	    tv.setText("0");
	    tv = (TextView) findViewById(R.id.valp4);
	    tv.setText("0");

	    tv = (TextView) findViewById(R.id.total);
	    tv.setText("0");
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, BUTTON_ID, 0, "Actualizar");
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case BUTTON_ID:
            Log.d("Seleccion", String.valueOf(base1.getSelectedItemPosition()));
            actualizar();
            return true;
    }
        return super.onMenuItemSelected(featureId, item);
    }
    
    public void sumar() {

    	float suma = 0;
	    TextView tv = (TextView) findViewById(R.id.valb1);
	    Log.d ("Texto",tv.getText().toString());
	    suma += Float.valueOf(tv.getText().toString());
	    tv = (TextView) findViewById(R.id.valb2);
	    suma += Float.valueOf(tv.getText().toString());
	    tv = (TextView) findViewById(R.id.valb3);
	    suma += Float.valueOf(tv.getText().toString());
	    tv = (TextView) findViewById(R.id.vala1);
	    suma += Float.valueOf(tv.getText().toString());
	    tv = (TextView) findViewById(R.id.vala2);
	    suma += Float.valueOf(tv.getText().toString());
	    tv = (TextView) findViewById(R.id.vala3);
	    suma += Float.valueOf(tv.getText().toString());
	    tv = (TextView) findViewById(R.id.vala4);
	    suma += Float.valueOf(tv.getText().toString());
	    tv = (TextView) findViewById(R.id.valp1);
	    suma += Float.valueOf(tv.getText().toString());
	    tv = (TextView) findViewById(R.id.valp2);
	    suma += Float.valueOf(tv.getText().toString());
	    tv = (TextView) findViewById(R.id.valp3);
	    suma += Float.valueOf(tv.getText().toString());
	    tv = (TextView) findViewById(R.id.valp4);
	    suma += Float.valueOf(tv.getText().toString());

	    DecimalFormat df = new DecimalFormat("0.00");

	    tv = (TextView) findViewById(R.id.total);
	    tv.setText(df.format(suma));
    }
    
    public void actualizar() {
	    PartidoParser partido = new PartidoParser();
		partido.setJugadores(bases, aleros, pivots);
	    partido.leerJS(244);
	    partido.leerJS(245);
	    partido.leerJS(246);
	    partido.leerJS(247);
	    partido.leerJS(248);
	    partido.leerJS(249);
	    partido.leerJS(250);
	    partido.leerJS(251);
	    partido.leerJS(252);
	    
	    float val, suma = 0;
	    
	    TextView tv = (TextView) findViewById(R.id.valb1);
	    val = bases[base1.getSelectedItemPosition()].getValSM();
	    tv.setText(Float.toString(val));
	    suma += val;
	    
	    tv = (TextView) findViewById(R.id.valb2);
	    val = bases[base2.getSelectedItemPosition()].getValSM();
	    tv.setText(Float.toString(val));
	    suma += val;
	    
	    tv = (TextView) findViewById(R.id.valb3);
	    val = bases[base3.getSelectedItemPosition()].getValSM();
	    tv.setText(Float.toString(val));
	    suma += val;
	    
	    tv = (TextView) findViewById(R.id.vala1);
	    val = aleros[alero1.getSelectedItemPosition()].getValSM();
	    tv.setText(Float.toString(val));
	    suma += val;
	    
	    tv = (TextView) findViewById(R.id.vala2);
	    val = aleros[alero2.getSelectedItemPosition()].getValSM();
	    tv.setText(Float.toString(val));
	    suma += val;
	    
	    tv = (TextView) findViewById(R.id.vala3);
	    val = aleros[alero3.getSelectedItemPosition()].getValSM();
	    tv.setText(Float.toString(val));
	    suma += val;
	    
	    tv = (TextView) findViewById(R.id.vala4);
	    val = aleros[alero4.getSelectedItemPosition()].getValSM();
	    tv.setText(Float.toString(val));
	    suma += val;
	    
	    tv = (TextView) findViewById(R.id.valp1);
	    val = pivots[pivot1.getSelectedItemPosition()].getValSM();
	    tv.setText(Float.toString(val));
	    suma += val;
	    
	    tv = (TextView) findViewById(R.id.valp2);
	    val = pivots[pivot2.getSelectedItemPosition()].getValSM();
	    tv.setText(Float.toString(val));
	    suma += val;
	    
	    tv = (TextView) findViewById(R.id.valp3);
	    val = pivots[pivot3.getSelectedItemPosition()].getValSM();
	    tv.setText(Float.toString(val));
	    suma += val;
	    
	    tv = (TextView) findViewById(R.id.valp4);
	    val = pivots[pivot4.getSelectedItemPosition()].getValSM();
	    tv.setText(Float.toString(val));
	    suma += val;
	    
	    DecimalFormat df = new DecimalFormat("0.00");
	    tv = (TextView) findViewById(R.id.total);
	    tv.setText(df.format(suma));

	    
//	    sumar();
    }
}
